package com.huiyou.control;

import com.huiyou.base.AppBack;
import com.huiyou.model.UserModel;
import com.huiyou.model.UserVerifyModel;
import com.huiyou.service.UserService;
import com.huiyou.service.UserVerifyService;
import com.huiyou.util.StringUtil;
import com.huiyou.util.TokenUtil;
import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(value = "APP接口")
@RestController
@EnableAutoConfiguration
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET}, value = "/api", produces = "application/json; charset=utf-8")
public class MobileController {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private UserVerifyService userVerifyService;


    @ExceptionHandler(Exception.class)
    public AppBack exceptionHandler(HttpServletRequest request, Exception e) {
        AppBack appBack = new AppBack(-97, "出现异常");

        String info = "";
        Map<String, String[]> map = request.getParameterMap();
        Set<Map.Entry<String, String[]>> set = map.entrySet();
        Iterator<Map.Entry<String, String[]>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<String, String[]> entry = it.next();
            info += "  | " + entry.getKey() + "  - ";
            for (String i : entry.getValue()) {
                info += i + "  ";
            }
        }
        appBack.setAction(request.getRequestURI() + info);
        appBack.setResult(e.getMessage());
        logger.error(request.getRequestURI());
        logger.error(e.getMessage(), e);
        return appBack;
    }


    @RequestMapping("/sendCode")
    public AppBack sendCode(@RequestParam String phone) {
        if (StringUtil.isEmptyString(phone)) {
            return new AppBack(-1, "必填信息不能为空");
        }
        AppBack<Map> appBack = userVerifyService.send(phone, 1);
        return appBack;
    }

    @RequestMapping("/register")
    public AppBack register(@RequestParam String phone,
                            @RequestParam String password,
                            @RequestParam String code) {
        Map condition = new HashMap();
        condition.put("phone", phone);
        condition.put("code", code);
        condition.put("type", 1);
        List<UserVerifyModel> userVerifyModels = userVerifyService.getList(condition);
        if (userVerifyModels.size() == 0) {
            return new AppBack(-1, "验证码错误或已过期");
        }
        String token = TokenUtil.getInstance().generateTokenUUID();
        List<UserModel> userModels = userService.getList(condition);
        if (userModels.size() > 0) {
            return new AppBack(-1, "已经被注册");
        }
        UserModel userModel = new UserModel();
        userModel.setPhone(phone);
        userModel.setPassword(password);
        userModel.setStatus(0);
        userModel.setToken(token);
        userModel.setRole(0);
        userModel.setNickname("用户"+phone);
        userModel.setMoney(0.0);
        userService.create(userModel);
        return new AppBack();
    }


}
